package com.msb.mall.thirdPart.service;

import com.msb.mall.thirdPart.utils.Upload2Server;
import com.msb.mall.thirdPart.constants.UploadConstants;
import com.msb.mall.thirdPart.enums.UploadPathServerEnum;
import com.msb.common.vo.CommonResult;
import com.msb.mall.thirdPart.vo.UploadServer;
import com.msb.mall.thirdPart.dto.UploadServerReqDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * TODO
 *
 * @author xulei
 * @date 2024/5/17 12:45
 */
@Slf4j
@Service
public class UploadServiceImpl {
    @Value("${uploadServer.host}")
    private String host;

    @Value("${uploadServer.user}")
    private String user;

    @Value("${uploadServer.password}")
    private String password;


    public CommonResult uploadServerSingle(UploadServerReqDTO reqDTO) {
        CommonResult commonResult = CommonResult.success();
        try {
            UploadServer uploadServer = getUploadServerSingle(reqDTO);
            Upload2Server.testUploadDebug(uploadServer);
        } catch (Exception e) {
            e.printStackTrace();
            commonResult.setCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            commonResult.setMsg(e.getMessage());
        }

        return commonResult;
    }

    /**
     * 多文件再另行扩展
     * @param reqDTO
     * @return
     */
    public CommonResult uploadServerMulti(UploadServerReqDTO reqDTO) {
        CommonResult commonResult = CommonResult.success();

        return commonResult;
    }

    /**
     * 初始化上传服务器对象，先处理好单文件
     * todo:多文件后续再处理
     * @param reqDTO
     * @return
     */
    public UploadServer getUploadServerSingle(UploadServerReqDTO reqDTO) {
        String uploadType = reqDTO.getUploadType();// 上传模块
        String operateType = reqDTO.getOperateType();// 操作类型

        UploadServer uploadServer = new UploadServer();
        uploadServer.setHost(host);
        uploadServer.setUser(user);
        uploadServer.setPassword(password);

        if(!UploadConstants.UPLOAD_OPR_LIST.contains(operateType)) {
            reqDTO.setOperateType(UploadConstants.UPLOAD_OPR_ADD); // 兜底处理，非需要的枚举值，默认是新增操作
        }

        String usePath = UploadPathServerEnum.getUsePath(uploadType);
        String noUsePath = UploadPathServerEnum.getNoUsePath(uploadType);
        if(UploadConstants.UPLOAD_OPR_ADD.equals(operateType)) {// 新增
            uploadServer.setUseFilePath(usePath);
            uploadServer.setFileList(reqDTO.getFileList());
        }

        if(UploadConstants.UPLOAD_OPR_UPDATE.equals(operateType)) {// 修改
            uploadServer.setUseFilePath(usePath);
            uploadServer.setNoUseFilePath(noUsePath);
            uploadServer.setFileList(reqDTO.getFileList());
            uploadServer.setDeletePaths(reqDTO.getDeletePaths());
        }

        if(UploadConstants.UPLOAD_OPR_DELETE.equals(operateType)) {// 删除
            uploadServer.setNoUseFilePath(noUsePath);
            uploadServer.setDeletePaths(reqDTO.getDeletePaths());
        }

        return uploadServer;
    }


    /**
     * 简单文件上传组件调用处理
     * @param uploadType
     * @return
     */
    public UploadServer getSimpleUploadServer(String uploadType) {

        UploadServer uploadServer = new UploadServer();
        uploadServer.setHost(host);
        uploadServer.setUser(user);
        uploadServer.setPassword(password);

        String operateType = UploadConstants.UPLOAD_OPR_ADD;

        String usePath = UploadPathServerEnum.getUsePath(uploadType);
        String noUsePath = UploadPathServerEnum.getNoUsePath(uploadType);
        if(UploadConstants.UPLOAD_OPR_ADD.equals(operateType)) {// 新增
            uploadServer.setUseFilePath(usePath);
        }

        if(UploadConstants.UPLOAD_OPR_UPDATE.equals(operateType)) {// 修改
            uploadServer.setUseFilePath(usePath);
            uploadServer.setNoUseFilePath(noUsePath);
//            uploadServer.setFileList(reqDTO.getFileList());
//            uploadServer.setDeletePaths(reqDTO.getDeletePaths());
        }

        if(UploadConstants.UPLOAD_OPR_DELETE.equals(operateType)) {// 删除
            uploadServer.setNoUseFilePath(noUsePath);
//            uploadServer.setDeletePaths(reqDTO.getDeletePaths());
        }

        return uploadServer;
    }

}
